﻿using Convertor.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.Sort
{
    public class Plugin : PluginBase, IEnumerable
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.Sort;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.Sort;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Array") + " - " + UILanguage.FindResource("Plugin_Sort_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_Sort_Desc"); }
        }

        

        

        #endregion

        #region IPluginConvert 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            var values = content.OutputContent as IEnumerable<object>;
            if (values == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_NotArray"), LogTypes.Alert); }
                return content;
            }
            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            object temp;
            switch (config.SortType)
            {
                default:
                case SortTypes.Alphabet:
                    temp = values.OrderBy(p => (p ?? string.Empty).ToString()).ToList();
                    break;
                case SortTypes.Reverse:
                    temp = values.Reverse().ToList();
                    break;
                case SortTypes.Length:
                    temp = values.OrderBy(p => (p ?? string.Empty).ToString().Length).ToList();
                    break;
            }
            if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_Sort_ConvertSuccess"), UILanguage.TryFindResource("Plugin_Sort_" + config.SortType)), LogTypes.Normal); }
            return new ConvertParamters(content.LastInputContent, temp);
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
